package io.renren.service.impl;

import com.alibaba.fastjson.JSONObject;
import io.renren.dao.EmpBaseinfoDao;
import io.renren.dao.PersonDispatchDao;
import io.renren.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.dao.EmpContractDao;
import io.renren.service.EmpContractService;



@Service("empContractService")
public class EmpContractServiceImpl implements EmpContractService {
	@Autowired
	private EmpContractDao empContractDao;

	@Autowired
	private PersonDispatchDao personDispatchDao;

	@Autowired
	private EmpBaseinfoDao empBaseinfoDao;
	
	@Override
	public EmpContractEntity queryObject(Integer id){
		return empContractDao.queryObject(id);
	}
	
	@Override
	public List<EmpContractEntity> queryList(Map<String, Object> map){
		return empContractDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return empContractDao.queryTotal(map);
	}
	
	@Override
	public void save(EmpContractEntity empContract){
		empContractDao.save(empContract);
	}

	@Override
	public void update(EmpContractEntity empContract){
		empContractDao.update(empContract);
	}
	
	@Override
	public void delete(Integer id){
		empContractDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		empContractDao.deleteBatch(ids);
	}

	@Override
	public void enableById(Integer id) {
		empContractDao.enableById(id);
	}

	@Override
	public void disableById(Integer id) {
		empContractDao.disableById(id);
	}

	@Override
	public void generatorContract(JSONObject jsonObject) {
		String idss = jsonObject.getString("ids");
		JSONObject json = (JSONObject)jsonObject.get("empContract");
		EmpContractEntity contract = json.toJavaObject(EmpContractEntity.class);
		String[] ids = idss.substring(1,idss.length()-1).split(",");
		SysUserEntity user = (SysUserEntity)jsonObject.get("user");
		for (Object id : ids) {
			id = id.toString().substring(1, id.toString().length() - 1);
			PersonDispatchEntity person = personDispatchDao.queryObject(id);
			EmpBaseinfoEntity empbase = empBaseinfoDao.queryObject(person.getPersonId());
			contract.setEmpCardno(empbase.getIdno());
			contract.setEmpName(empbase.getName());
			contract.setEmpNo(empbase.getId()+"");
			contract.setOperatePerson(user.getUsername());
			contract.setIfEffective("0");
			empContractDao.saveByDynamicCol(contract);
		}
	}

	@Override
	public void simpleAudit(JSONObject params, SysUserEntity sysUserEntity) {
		EmpContractEntity empContractEntity = new EmpContractEntity();
		empContractEntity.setId(Integer.parseInt(params.get("id").toString()));
		empContractEntity.setAudit(params.get("audit").toString());
		empContractEntity.setAuditperson(sysUserEntity.getUsername());
		empContractEntity.setAuditdate(new Date());
		empContractDao.simpleAudit(empContractEntity);

	}
}
